package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自动填写属性的切面类
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 定义切入点  切入点表达式不能单纯是那个包下的什么什么类，那么那样太粗糙了
     * 所以我们定义注解形式的切入点
     */
    @Pointcut("execution(* *.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void aotoFillPoingcut(){

    }

    @Before("aotoFillPoingcut()")
    public void aotoFile(JoinPoint joinPoint){
        log.info("进来了契入点表达式之前,{}",joinPoint);


        //获取切入点表达式上方的注解对象，
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        //判断注解对象的操作类型
        OperationType value = annotation.value();
        //准备数据
        Object[] args = joinPoint.getArgs();
        if(args[0] == null){
            return;
        }
        Object arg = args[0];
        LocalDateTime now = LocalDateTime.now();
        Long id = BaseContext.getCurrentId();
        //加入数据
        if(value==OperationType.INSERT){
            //insert
            try {
                //获取方法
                Method setCreateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //反射调用方法
                setCreateTime.invoke(arg,now);
                setCreateUser.invoke(arg,id);
                setUpdateTime.invoke(arg,now);
                setUpdateUser.invoke(arg,id);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(value==OperationType.UPDATE){
            //insert
            try {
                //获取方法
                Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //反射调用方法
                setUpdateTime.invoke(arg,now);
                setUpdateUser.invoke(arg,id);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }



}
